package com.wingscode.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wingscode.common.utils.PageUtils;
import com.wingscode.common.utils.Query;
import com.wingscode.modules.sys.dao.DictionaryValueDao;
import com.wingscode.modules.sys.entity.DictionaryValueEntity;
import com.wingscode.modules.sys.service.DictionaryValueService;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 字典value
 *
 * @author yan
 */
@Service("dictionaryValueService")
public class DictionaryValueServiceImpl extends ServiceImpl<DictionaryValueDao, DictionaryValueEntity> implements DictionaryValueService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		Integer pid = null;
		String name = "";

		if (params.containsKey("name"))
			name = (String)params.get("name");
		if (params.containsKey("pid") && params.get("pid") != null && !params.get("pid").toString().isEmpty())
			pid = Integer.parseInt(params.get("pid").toString());

		IPage<DictionaryValueEntity> page = this.page(
			new Query<DictionaryValueEntity>().getPage(params),
			new QueryWrapper<DictionaryValueEntity>()
				.like(StringUtils.isNotBlank(name),"name", name)
				.eq(pid != null, "pid", pid)
				.orderByAsc("sort")
				.orderByDesc("id")
		);

		return new PageUtils(page);
	}
	
	@Override
	public List<DictionaryValueEntity> getList(Map<String, Object> params) {
		String type = "";
		Integer pid = null;

		if (params.containsKey("type"))
			type = (String)params.get("type");
		if (params.containsKey("pid") && params.get("pid") != null && !params.get("pid").toString().isEmpty())
			pid = (Integer) params.get("pid");

		return baseMapper.getList(type, pid);
	}

	@Override
	public void deleteBatch(Long[] Id) {
		this.removeByIds(Arrays.asList(Id));
	}

	@Override
	public PageUtils queryByType(Map<String, Object> params) {
		String name = "", type = "";

		if (params.containsKey("name"))
			name = (String)params.get("name");
		if (params.containsKey("type"))
			type = (String)params.get("type");

		IPage<DictionaryValueEntity> page = this.baseMapper.queryByType(new Query<DictionaryValueEntity>().getPage(params), type, name);

		return new PageUtils(page);
	}
}
